<?php
namespace app\index\controller;
use think\Controller;
use think\Config;

class Index extends Controller
{
    public function index()
    {
        echo "Hello Food Card!!!";
    }

    public function getCodeInfo() {
    	$code = $_GET['code'];
    	$appId = Config::get('common_config.app_id');
    	$appSecret = Config::get('common_config.app_secret');

    	$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
    	$res = json_decode(httpGet($url));

    	if (property_exists($res, 'errcode')) {
    		$arr = array(
    			"errcode" => $res->errcode,
    			"errmsg" => $res->errmsg
    		);
    	} else {
    		$arr = array(
    			"session_key" => $res->session_key,
    			"openid" => $res->openid
    		);
            $this->saveUserInfo($res->openid);
    	}

    	echo json_encode($arr);
    	exit();
    }

    public function getACode()
    {

    	$page = $_GET['page'];
    	$scene = $_GET['scene'];

        $appId = Config::get('common_config.app_id');
        $appSecret = Config::get('common_config.app_secret');
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
        $res = json_decode(httpGet($url));
        $accessToken = $res->access_token;

        $requestAccessTokenUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
        $postData = array(
            "page" => $page,
            "is_hyaline" => true,
            "scene" => $scene
            );
        $requestData = json_encode($postData);
        $data = httpPost($requestAccessTokenUrl, $requestData);

        header('content-type:image/jpeg');
        echo $data;

        exit();
    }


    // 初始化保存用户信息
    protected function saveUserInfo($openid) {
        $user = model('User');
        $res = $user->where('openid', $openid)->find();
        if (!$res) {
            $user->data([
                'openid' => $openid
            ]);
            $user->save();
        }
    }


    public function getBannerInfo() {
        $bannerConfig = model('Configuration')->where('key', 'banner_num')->find();
        $bannerNum = (int)($bannerConfig->value);

        $banners = model('BannerConf')->limit($bannerNum)->select();
        // echo "<pre>";
        // var_dump($banners);
        echo json_encode($banners);
        exit();
    }


    public function getTel() {
        $tel = model('Configuration')->where('key', 'tel')->find()->value;
        echo json_encode($tel);
        exit();
    }


    public function verifyAuthCode() {
        $authCode = $_POST['auth_code'];
        $res = array('code' => 0, 'tip' => "无效的授权码");
        if ($code = model('AuthCode')->where('code', $authCode)->find()) {
            if ($code->is_use == '0') {
                $res['code'] = 1;
                $res['tip'] = "授权成功";
            } else {
                $res['tip'] = "授权码已过期";
            }
        }
        echo json_encode($res);
        exit();
    }


}
